import { Component, OnInit } from '@angular/core';
import {ApiService} from "@core/services/http/api.service";
import {GridConfigService} from "@core/config/grid-config.service";
import {GridOptions} from '@ag-grid-community/core';

@Component({
  selector: 'program',
  templateUrl: './program.component.html',
  styleUrls: ['./program.component.scss']
})
export class ProgramComponent implements OnInit {
  /**容器高度**/
  _height: number = 0;
  /**按钮列表**/
  btnBar = [
    { style: 'default', label: '创建', operationId: 'action_', api: '', loading: false },
    { style: 'default', label: '编辑', operationId: 'action_', api: '', loading: false },
    { style: 'default', label: '查询', operationId: 'action_query', api: '', loading: false },
  ];
  /**状态列表**/
  statusBar = [];

  /**grid区域**/
  rowData: any[] = [];
  gridOptions: GridOptions;
  columnDefs: any[] = [];
  localeText: any;

  /**上下文环境变量**/
  ctx: any = {};

  constructor( protected apiService: ApiService,
               private gridConfigService: GridConfigService,) {
    this.columnDefs = [
      {headerName: '小程序名称', field: 'name', width: 120 },
      {headerName: '部件', field: 'widget.id', width: 120 },
      {headerName: '调用接口', field: 'widgetApi.id', width: 120 },
      {headerName: '刷新周期', field: 'period', width: 120 },
    ];
    this.localeText = gridConfigService.getLocaleText();
  }

  ngOnInit() {
    this._height = document.querySelector('.content').clientHeight;
    // this.searchForm = new FormGroup({quickSearchInput: new FormControl(''),});
    this.getSearchFormMetadata()
  }


  /**
   * 功能：按钮事件路由
   * @param btnMetadata 当前按钮元数据
   */
  onEvent(btnMetadata: any) {
    if (btnMetadata instanceof MouseEvent) return;
    if (btnMetadata) {
      // if (btnMetadata.id === 'action_new') {
      //   this.action_new(btnMetadata);
      //   this.btnInit(btnMetadata);
      // } else if (btnMetadata.id === 'action_cancel') {
      //   this.showConfirm('是否放弃当前在编辑的数据', btnMetadata);
      // } else if (btnMetadata.id === 'action_search') {
      //   this.action_search(btnMetadata);
      //   this.btnInit(btnMetadata);
      // } else if (btnMetadata.wf === 'true') {
      //   this.action_oa(btnMetadata);
      // } else {
      //   if (btnMetadata.confirm === 'true') {
      //     this.showConfirm('是否执行 [' + btnMetadata.label + '] 操作？', btnMetadata);
      //   } else {
      //     this.submit(btnMetadata);
      //   }
      // }
    }
  }

  /**
   * 功能：获取表单元数据
   */
  getSearchFormMetadata() {
    this.apiService.post('bi.applet.findAll', {}).subscribe(
        (body) => {
          if (body) {
            console.log(body)
            this.rowData = body.content;
          }
        });
  }

}
